<template>
	<view class="body">
		<uni-section title="门禁1" padding="20rpx 40rpx;">
			<view class="row-input">
				<text class="star">*</text>
				<text class="label">房间号:</text>
				<input type="text" class="editor" placeholder="例: 303、大门" maxlength="20" v-model="room_num1" />
			</view>
			<view class="row-input mt-10">
				<text class="star">*</text>
				<text class="label">门禁编号:</text>
				<input type="text" class="editor" placeholder="向负责人索取" maxlength="30" v-model="gate_num1" />
			</view>
			<view class="row-input mt-10">
				<text class="star">*</text>
				<text class="label">管理账号:</text>
				<input type="text" class="editor" placeholder="向负责人索取" maxlength="30" v-model="admin1" />
			</view>
			<view class="row-input mt-10">
				<text class="star">*</text>
				<text class="label">管理密码:</text>
				<input type="text" class="editor" placeholder="向负责人索取" maxlength="30" v-model="pwd1">
			</view>
			<view class="row-input mt-10">
				<text class="star">*</text>
				<text class="label">门禁品牌:</text>
				<radio-group @change="factoryCodeChanged" class="editor no-border"
					style="display: flex;flex-direction: row;">
					<label class="radio">
						<radio value="dahua" :checked="factoryCode=='dahua'" />大华
					</label>
					<view style="width: 40rpx;"></view>
					<label class="radio">
						<radio value="hikvision" :checked="factoryCode=='hikvision'"/>海康
					</label>
				</radio-group>
			</view>
			<view class="row-input mt-10">
				<text class="star">*</text>
				<text class="label">房间类型:</text>
				<radio-group @change="roomTypeChanged" class="editor no-border"
					style="display: flex;flex-direction: row;">
					<label class="radio">
						<radio value="0" :checked="roomType==0" />默认
					</label>
					<view style="width: 40rpx;"></view>
					<label class="radio">
						<radio value="1" :checked="roomType==1" />会议室
					</label>
				</radio-group>
			</view>
			<view class="row-input mt-10" v-if="roomType==1">
				<text class="star hide">*</text>
				<text class="label">容纳人数:</text>
				<input type="text" class="editor" placeholder="填写数量" maxlength="4" v-model="capacity" />
			</view>
		</uni-section>

		<button type="primary" class="btn" @click="save">保存</button>
	</view>
</template>

<script>
	import cfg from '@/utils/config.js'
	import MemItem from '@/components/mem_item/mem_item.vue';
	export default {
		components: {
			MemItem
		},
		data() {
			return {
				id: null,
				room_num1: '',
				gate_num1: '',
				admin1: '',
				pwd1: '',
				factoryCode: 'dahua', //hikvision
				
				roomType: 0, //1默认  1会议室
				capacity: null,
			}
		},
		onLoad(param) {
			this.id = param.id
		},
		onShow() {
			if (this.id != null && this.id != '') {
				this.queryInfo()
			}
		},
		methods: {
			roomTypeChanged(e) {
				this.roomType = e.detail.value
			},
			factoryCodeChanged(e) {
				this.factoryCode = e.detail.value
			},
			queryInfo() {
				uni.request({
					url: cfg.host + 'room/get/' + this.id,
					success: (res) => {
						if (res.data.code == 0) {
							let data = res.data.data
							if (data != null) {
								this.room_num1 = data.name
								this.roomType = data.type
								this.capacity = data.capacity
								
								let dev = data.device
								if (dev) {
									this.gate_num1 = dev.serialNumber
									this.admin1 = dev.username
									this.pwd1 = dev.password
									this.factoryCode = dev.factoryCode
								}
							}
						}
					}
				})
			},
			save() {
				if (!this.room_num1.trim()) {
					uni.showToast({
						title: '请填写房间号/名称',
						icon: 'none'
					})
					return
				}
				if (!this.gate_num1.trim()) {
					uni.showToast({
						title: '请填写门禁编号',
						icon: 'none'
					})
					return
				}
				if (!this.admin1.trim()) {
					uni.showToast({
						title: '请填写管理账号',
						icon: 'none'
					})
					return
				}
				if (!this.pwd1.trim()) {
					uni.showToast({
						title: '请填写管理密码',
						icon: 'none'
					})
					return
				}
				if(this.factoryCode==null){
					uni.showToast({
						title: '请选择门禁品牌',
						icon: 'none'
					})
					return
				}
				if(this.roomType==null){
					uni.showToast({
						title: '请选择房间类型',
						icon: 'none'
					})
					return
				}

				uni.request({
					url: cfg.host + 'room/save',
					method: 'POST',
					data: {
						id: this.id,
						name: this.room_num1,
						type:this.roomType,
						capacity:this.capacity,
						device: {
							serialNumber: this.gate_num1,
							username: this.admin1,
							password: this.pwd1,
							factoryCode:this.factoryCode
						}
					},
					success: (res) => {
						if (res.data.code == 0) {
							uni.showToast({
								title: '保存成功',
								icon: 'success'
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							let msg = res.data.msg || '保存失败'
							uni.showToast({
								title: msg,
								icon: 'error'
							})
						}
					},
					fail: () => {
						uni.showToast({
							title: '保存失败',
							icon: 'error'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.ic-gate-add {
		width: 50rpx;
		height: 50rpx;
	}

	.member-wrap {
		display: flex;
		flex-direction: column;
	}
</style>